//
//  ImageScrollView.m
//  ZAKER
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ImageScrollView.h"

@interface ImageScrollView ()<UIScrollViewDelegate>

{
    
    NSInteger _pageCount; // 记录图片的数量
    
}

@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) NSTimer *timer;


@end

@implementation ImageScrollView

-(void)dealloc
{
    
    [_page release];
    [_scroll release];
    [_timer release];
    
    [super dealloc];
    
}

-(instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
    }
    return self;
    
}

// 懒加载定时器
- (NSTimer *)timer
{
    if (_timer == nil) {
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
    
    return _timer;
    
}

#pragma mark - 轮播图的scrollView, 以及协议方法

-(void)createImageScroll
{
    
    self.scroll = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200 * myHeight)] autorelease];
    _scroll.pagingEnabled = YES;
    _scroll.contentSize = CGSizeMake(self.frame.size.width * (_pageCount + 2), 0);
    _scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    _scroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:_scroll];
    _scroll.delegate = self;
    _scroll.bounces = NO;
    [_scroll release];
    [self timer];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 
    //先让计时器停止
    [self.timer invalidate];
    
    /* 判断循环滚动 */
    if (scrollView.contentOffset.x == 0) {
        
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * _pageCount, 0);
        
    }else if (scrollView.contentOffset.x == scrollView.frame.size.width * (_pageCount + 1)){
        
        scrollView.contentOffset = CGPointMake(scrollView.frame.size.width, 0);
        
    }
    
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.page.currentPage = index - 1;
    
    self.timer = nil;
    [self timer];
    
}



#pragma merk - 创建page

-(void)createPage
{
    
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(_scroll.frame.size.width - 220 * myWidth, 180 * myHeight, 70 * myWidth, 30 * myHeight)] ;
    self.page.numberOfPages = _pageCount;
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
    self.page.pageIndicatorTintColor = [UIColor grayColor];
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.currentPage = 0;
    [self addSubview:self.page];
    [_page release];

}

#pragma mark - 轮播图model赋值的方法
-(void)setImageWithModels:(NSArray *)array
{
    
    // 先去记录图片的数量, 因为这个array是循环滚动的所有图片数量,所以-2
    _pageCount = [array count] - 2;
    [self createImageScroll];
    [self createPage];
    
    for (int i = 0; i < array.count; i++) {
        SubscriptionModel *model = [array objectAtIndex:i];
        MyImageView *image = [[MyImageView alloc]initWithFrame:CGRectMake(i * self.scroll.frame.size.width, 0, self.scroll.frame.size.width, self.scroll.frame.size.height)];
        image.userInteractionEnabled = YES;
        [image sd_setImageWithURL:[NSURL URLWithString:model.promotion_img] placeholderImage:nil];
        image.model = model;
        [_scroll addSubview:image];
        image.target = self;
        image.action = @selector(imageAction:);
        [image release];
        
    }
}


#pragma mark - 点击方法

#pragma mark ** 定时器实现方法

- (void)timerAction
{
    
    CGPoint point = CGPointMake(self.scroll.contentOffset.x + self.scroll.frame.size.width, 0);
    [self.scroll setContentOffset:point animated:YES];
    
    // 图片自动滚动后判断偏移量,如果是最后一张图就直接设置偏移量到第二张图.实现无限滚动
    if (self.scroll.contentOffset.x == (_pageCount + 1) * self.scroll.frame.size.width){
        
        self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width, 0);
        
    }
    
    // page与图片同步
    if (self.scroll.contentOffset.x == _pageCount * self.frame.size.width) {
        
        self.page.currentPage = 0;
        
    } else {
        
        self.page.currentPage = self.scroll.contentOffset.x / self.frame.size.width;
        
    }
}


#pragma mark ** page的点击方法

- (void)pageAction:(id)sender
{
    
    NSInteger index = self.page.currentPage * _scroll.frame.size.width;
    _scroll.contentOffset = CGPointMake(index, 0);
    
}

#pragma mark ** 照片的target/action

-(void)imageAction:(MyImageView *)image
{
    
// 在这里告诉 subsciptionViewController 我点击了顶部图片
    [(NSObject *)self.target performSelector:self.action withObject:image];
    
}




@end
